package nimy.info.android.dinnertime.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import nimy.info.android.dinnertime.R;
import nimy.info.android.dinnertime.exception.ExceptionHandler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

public class Utils {

	private static final String DEBUG_TAG = "Activity Utils ";

	private static final int[] KEYWORDS_COLORS = { 0xAAFF0000, 0xAAFF0000,
			0xAAFF0000, 0xAAFFFF00, 0xAAFF00FF, 0xAAFE0FE0, 0xAA00FF00,
			0xAA0000FE, 0xAAE00FCD, 0xAAE00F0D };

	public static Properties getNetConfigProperties() {
		Properties props = new Properties();
		InputStream in = Utils.class
				.getResourceAsStream("/dinnertime.properties");
		try {
			props.load(in);
		} catch (IOException e) {
			ExceptionHandler.justLog(Utils.class, e,
					"Can not read the properties: 'dinnertime.properties'");
		}
		return props;
	}

	public static String getProperty(final String key, final String defaultValue) {
		return getNetConfigProperties().getProperty(key, defaultValue);
	}

	public static boolean isBlank(final String keyword) {
		return keyword != null && !keyword.trim().equals("");
	}

	public static final <T> List<T> emptyList() {
		return Collections.emptyList();
	}

	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager conMan = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();

		boolean mobileConnected = mobile == State.CONNECTED
				|| mobile == State.CONNECTING;
		Log.d(DEBUG_TAG, "Wifi connected: " + mobileConnected);
		if (mobileConnected)
			return true;
		boolean wifiConnected = wifi == State.CONNECTED
				|| wifi == State.CONNECTING;
		Log.d(DEBUG_TAG, "Mobile connected: " + wifiConnected);
		if (wifiConnected)
			return true;
		return false;
	}

	public static void gotoConfigureConnection(Activity context) {
		context.startActivityForResult(new Intent(
				Settings.ACTION_WIRELESS_SETTINGS), 1);
	}

	public static void drawBackground(final Activity context,
			final Canvas canvas) {
		Bitmap playingBackground = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.background);
		View pageView = context.findViewById(R.id.network_disable_page);

		int w = pageView.getBackground().getBounds().width();
		int h = pageView.getBackground().getBounds().height();

		DisplayMetrics metrics = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metrics);

		int phoneWidth = metrics.widthPixels;
		int phoneHeigth = metrics.heightPixels;

		float scaleWidth = ((float) phoneWidth / w);
		float scaleHeight = ((float) phoneHeigth / h);

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		canvas.drawBitmap(playingBackground, matrix, new Paint());
	}

	public static Toast toastAtCenter(Context context, String text, int howLong) {
		Toast toast = Toast.makeText(context, text, howLong);
		toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2,
				toast.getYOffset() / 2);
		toast.show();
		return toast;
	}

	public static int getRandomColor(int count) {
		if (count < 10 && count >= 0) {
			return KEYWORDS_COLORS[count];
		}
		Double d = Math.random() * count;
		String countString = Integer.toString(d.intValue());
		int colorIndex = Integer.parseInt(countString.substring(countString
				.length() - 1));
		return KEYWORDS_COLORS[colorIndex];
	}
}